package ch.ethz.fcl.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
import android.util.Log;
 
public class UDPClient implements Runnable {
	private final String LOG_TAG = "UDP CLIENT";
	private static String message = "Hello from Client";
	public static String ip = "192.168.1.3";
	
	public UDPClient(String msg){
		message = msg;
	}
	
    public void run() {
        try {
            // Retrieve the ServerName
            InetAddress serverAddr = InetAddress.getByName(ip);
            Log.d(LOG_TAG, "C: Connecting to ip: "+ip);
            /* Create new UDP-Socket */
            DatagramSocket socket = new DatagramSocket();
            byte[] buf = message.getBytes();
            
            DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, UDPServer.SERVERPORT);
            Log.d(LOG_TAG, "C: Sending: '" + new String(buf) + "'");
            
            socket.send(packet);
            Log.d(LOG_TAG, "C: Sent.");
        } catch (Exception e) {
            Log.e(LOG_TAG, "C: Error", e);
        }
    }
    
    public void setMessage(String msg){
    	message = msg;
    }
    
    public void setMessage(int msg){
    	message = msg+"";
    }
}
